//============================================================================================
/**
 * @file	wbtower_codein.c
 * @brief	WIFIバトルタワー数値入力処理
 * @author	Akito Mori
 * @date	08.12.15
 */
//============================================================================================


#include "common.h"
#include <dwc.h>
#include "libdpw/dpw_bt.h"
#include "system/procsys.h"
#include "system/arc_tool.h"
#include "system/arc_util.h"
#include "system/clact_tool.h"
#include "system/msgdata_util.h"
#include "system/wordset.h"
#include "msgdata/msg.naix"
#include "system/wipe.h"
#include "system/fontproc.h"
#include "system/lib_pack.h"
#include "system/lib_pack.h"
#include "system/fontoam.h"
#include "system/window.h"
#include "gflib/touchpanel.h"
#include "system/bmp_menu.h"
#include "system/snd_tool.h"
#include "gflib/strbuf_family.h"
#include "savedata/savedata.h"
#include "savedata/wifilist.h"
#include "savedata/zukanwork.h"
#include "gflib/strbuf.h"
#include "communication/comm_state.h"
#include "communication/comm_def.h"

#include "application/wbtower.h"
#include "wbtower_local.h"
#include "field/b_tower_wifi.h"
#include "wifi/dwc_rap.h"

#include "msgdata/msg_wifi_lobby.h"
#include "msgdata/msg_wifi_bt.h"
#include "msgdata/msg_wifi_system.h"

#include "savedata/email_savedata.h"

#include "application/connect_anm.h"


#include "../wifi_worldtrade/worldtrade.naix"			// グラフィックアーカイブ定義
#include "../wifi_p2pmatch/wifip2pmatch.naix"			// グラフィックアーカイブ定義
#include "btower_codein.naix"



//==============================================================================
// 各種定義
//==============================================================================
enum{
	BTOWER_CODEIN_START_FADEOUT=0,
	BTOWER_CODEIN_START_FADEOUT_WAIT,
	BTOWER_CODEIN_FADEIN_WAIT,
	BTOWER_CODEIN_MAIN,
	BTOWER_CODEIN_DECIDE_MES,
	BTOWER_CODEIN_DECIDE_YESNO,
	BTOWER_CODEIN_DECIDE_YESNO_WAIT,
	BTOWER_CODEIN_FADEOUT,
	BTOWER_CODEIN_FADEOUT_WAIT,
	BTOWER_CODEIN_END_FADEIN_WAIT,
	BOTWER_CODEIN_BUTTON_ANM,
	BTOWER_CODEIN_END,
	BTOWER_CODEIN_MAX,
};

enum{
	CODEIN_SEQ_CONTINUE=0,
	CODEIN_SEQ_END,
};

enum{
	CURSOR_ROW_LEFT=0,
	CURSOR_ROW_RIGHT,
};

#define CODEIN_POINT_100	(  0 )
#define CODEIN_POINT_10		(  1 )
#define CODEIN_POINT_1		(  2 )
#define CODEIN_NUM_0		(  3 )
#define CODEIN_NUM_1		(  4 )
#define CODEIN_NUM_2		(  5 )
#define CODEIN_NUM_3		(  6 )
#define CODEIN_NUM_4		(  7 )
#define CODEIN_NUM_5		(  8 )
#define CODEIN_NUM_6		(  9 )
#define CODEIN_NUM_7		( 10 )
#define CODEIN_NUM_8		( 11 )
#define CODEIN_NUM_9		( 12 )
#define CODEIN_MODORU		( 13 )	// 「もどる」
#define CODEIN_DECIDE		( 14 )	// 「けってい」
#define CODEIN_CANCEL		( 15 )	// 「キャンセル」

//==============================================================================
// 関数プロトタイプ宣言
//==============================================================================

static int Codein_start_fadeout( WBTOWER_WORK *wk );
static int Codein_start_fadeout_wait( WBTOWER_WORK *wk );
static int Codein_fadein_wait( WBTOWER_WORK *wk );
static int Codein_main( WBTOWER_WORK *wk );
static int Codein_decide_mes( WBTOWER_WORK *wk );
static int Codein_decide_yesno( WBTOWER_WORK *wk );
static int Codein_decide_yesno_wait( WBTOWER_WORK *wk );
static int Codein_fadeout( WBTOWER_WORK *wk );
static int Codein_fadeout_wait( WBTOWER_WORK *wk );
static int Codein_end_fadein_wait( WBTOWER_WORK *wk );
static int Codein_end( WBTOWER_WORK *wk );
static int Codein_button_anm( WBTOWER_WORK *wk );

static  u32 KeyControl( WBTOWER_WORK *wk );
static void DecideFunc( WBTOWER_WORK *wk, int cursor );
static  int next_cursor_pos( int pos, int arrow, u16 *work );
static  u32 TouchControl( WBTOWER_WORK *wk );
static void CodeActShow( WBTOWER_WORK *wk );
static void BmpWinInit( WBTOWER_WORK *wk );
static void BmpWinDelete( WBTOWER_WORK *wk );
static void ButtonMsgDraw( GF_BGL_BMPWIN *win, MSGDATA_MANAGER *msgman, int msgno );
static  int num_max_check( WBTOWER_CODEIN_WORK *wcw, int input );
static  int get_codein_num( WBTOWER_CODEIN_WORK *wcw );
static void CodeinEnd( WBTOWER_WORK *wk );
static  int end_check( WBTOWER_CODEIN_WORK *wcw );
static  int no_input_check( WBTOWER_CODEIN_WORK *wcw );
static void ButtonAnmCore( WBTOWER_WORK * wk, u8 pal );
static BOOL ButtonAnmMain( WBTOWER_WORK * wk );
static void ObjButtonAnmSet( WBTOWER_WORK * wk, u8 id, u8 anm1, u8 anm2, u16 next );
static void BgButtonAnmSet( WBTOWER_WORK * wk, u8 px, u8 py, u16 next );
static void  _num_obj_anime( WBTOWER_WORK *wk, int act );
static void ClactInit( WBTOWER_WORK *wk );


static int (*FuncTbl[BTOWER_CODEIN_MAX])( WBTOWER_WORK *wk) = {
	Codein_start_fadeout,
	Codein_start_fadeout_wait,
	Codein_fadein_wait,
	Codein_main,
	Codein_decide_mes,
	Codein_decide_yesno,
	Codein_decide_yesno_wait,
	Codein_fadeout,
	Codein_fadeout_wait,
	Codein_end_fadein_wait,
	Codein_button_anm,
	Codein_end,

};

//==============================================================================
/**
 * @brief   数値入力処理初期化
 *
 * @param   wk
 * @param   mode	終了時にフェードアウトで終わるか、復帰させるか
 *                  （BTWOWER_CODEIN_END_FADEOUT, BTOWER_CODEIN_END_FADEIN)
 * @param   max		入力できる数値のマックス（ 〜10か、〜50か、〜100か）
 *
 * @retval  int
 */
//==============================================================================
int WBTower_CodeinInit( WBTOWER_WORK *wk, int mode, int max )
{
//	max = 100;
	wk->wcw.mode 	= mode;
	wk->wcw.max  	= max;
	wk->wcw.num  	= 0;
	wk->wcw.cursor = 0;		// カーソル位置初期化
	if(max!=100){
		wk->wcw.point  = 1;	// 数値入力ポイントはmaxで変わる
		wk->wcw.keta   = 1;
	}else{
		wk->wcw.point  = 0;
		wk->wcw.keta   = 0;
	}
	wk->wcw.seq    = BTOWER_CODEIN_START_FADEOUT;
	wk->wcw.fig[0] = -1;
	wk->wcw.fig[1] = -1;
	wk->wcw.fig[2] = -1;

	wk->wcw.cur_work[0] = CURSOR_ROW_LEFT;
	wk->wcw.cur_work[1] = CURSOR_ROW_RIGHT;

    if(wk->CursorActWork != NULL){  //一応
    	WBTower_CursorActPos( wk->CursorActWork, 0 );
    }

	return 1;
}


//==============================================================================
/**
 * @brief   数値入力メイン
 *
 * @param   wk
 *
 * @retval  int
 */
//==============================================================================
int WBTower_CodeinMain( WBTOWER_WORK *wk )
{
	int ret = FuncTbl[wk->wcw.seq](wk);

	if(ret==CODEIN_SEQ_END){
		return wk->wcw.num;
	}
	return 0;
}






// -----------------------------------------------------------------
// -----------------------------------------------------------------
// -----------------------------------------------------------------
// -----------------------------------------------------------------
// -----------------------------------------------------------------

static int Codein_start_fadeout( WBTOWER_WORK *wk )
{
	// 下画面フェードインスタート
	WIPE_SYS_Start( WIPE_PATTERN_S, WIPE_TYPE_FADEOUT, WIPE_TYPE_FADEOUT, WIPE_FADE_BLACK, 4, 1, HEAPID_WIFI_BATTLETOWER );

	wk->wcw.seq = BTOWER_CODEIN_START_FADEOUT_WAIT;

	return CODEIN_SEQ_CONTINUE;

}

static int Codein_start_fadeout_wait( WBTOWER_WORK *wk )
{
	if(WIPE_SYS_EndCheck()==TRUE){

		ClactInit(wk);

		WBTower_PointActPos( wk->PointActWork, wk->wcw.point, wk->wcw.keta );

		WBTower_SubBgGraphicSet( wk, 1 );

		// BMPWIN確保
		BmpWinInit( wk );
		// 文字列描画
		ButtonMsgDraw( &wk->wcw.ButtonMsgWin[0], wk->MsgManager, msg_wifi_bt_codein_001 );
		ButtonMsgDraw( &wk->wcw.ButtonMsgWin[1], wk->MsgManager, msg_wifi_bt_codein_002 );
		ButtonMsgDraw( &wk->wcw.ButtonMsgWin[2], wk->MsgManager, msg_wifi_bt_codein_003 );
		// ＢＧ面表示ＯＮ
		GF_BGL_VisibleSet( GF_BGL_FRAME0_S, VISIBLE_ON );
	//	GF_BGL_VisibleSet( GF_BGL_FRAME0_S, VISIBLE_ON );

		// 下画面フェードインスタート
		WIPE_SYS_Start( WIPE_PATTERN_S, WIPE_TYPE_FADEIN, WIPE_TYPE_FADEIN, WIPE_FADE_BLACK, 4, 1, HEAPID_WIFI_BATTLETOWER );

		wk->wcw.seq = BTOWER_CODEIN_FADEIN_WAIT;

	}
	return CODEIN_SEQ_CONTINUE;

}


//------------------------------------------------------------------
/**
 * @brief   数字OBJ、カーソルOBJ、表示ONと座標位置調整
 *
 * @param   wk
 *
 * @retval  none
 */
//------------------------------------------------------------------
static void ClactInit( WBTOWER_WORK *wk )
{
	int i;
	// 入力に使用するセルアクターの表示ON
	if(wk->wcw.max==100){
		CLACT_SetDrawFlag( wk->ArrowActWork[0], 1 );
	}
	CLACT_SetDrawFlag( wk->ArrowActWork[1], 1 );
	CLACT_SetDrawFlag( wk->ArrowActWork[2], 1 );

	// ２桁・３桁の時に入力数値位置を変更する（センタリング）
	for(i=0;i<3;i++){
		WBTower_NumActPos( wk->ArrowActWork[i], i, wk->wcw.keta );
	}

	CLACT_SetDrawFlag( wk->CursorActWork, 1 );
	CLACT_SetDrawFlag( wk->PointActWork, 1 );
	CLACT_SetDrawFlag( wk->ButtonActWork[0], 1 );
	CLACT_SetDrawFlag( wk->ButtonActWork[1], 1 );
	CLACT_SetDrawFlag( wk->ButtonActWork[2], 1 );

}

//------------------------------------------------------------------
/**
 * @brief   フェードイン待ち
 *
 * @param   WK
 *
 * @retval  int
 */
//------------------------------------------------------------------
static int Codein_fadein_wait( WBTOWER_WORK *wk )
{
	if(WIPE_SYS_EndCheck()==TRUE){
		wk->wcw.seq = BTOWER_CODEIN_MAIN;
	}

	return CODEIN_SEQ_CONTINUE;
}


// タッチ範囲設定
static RECT_HIT_TBL CodeinTouchTbl[][17]={
	// ３桁の時
	{
		{  14,  47,  88, 119,},		// 入力済み数値のタッチ位置
		{  14,  47, 120, 151,},
		{  14,  47, 152, 183,},

		{  60,  99,  12,  51 },		// ０〜９
		{  60,  99,  60,  99 },
		{  60,  99, 108, 147 },
		{  60,  99, 158, 195 },
		{  60,  99, 204, 243 },
		{ 108, 147,  12,  51 },
		{ 108, 147,  60,  99 },
		{ 108, 147, 108, 147 },
		{ 108, 147, 158, 195 },
		{ 108, 147, 204, 243 },

		{ 164, 187,   4,  75 },	// 一番下のボタン３つ
		{ 164, 187,  92, 163 },
		{ 164, 187, 180, 251 },

		{TP_HIT_END,0,0,0},		// 終了データ
	},
	// ２桁の時
	{
		{  14,  47,  88-16, 119-16,},		// 入力済み数値のタッチ位置
		{  14,  47, 120-16, 151-16,},
		{  14,  47, 152-16, 183-16,},

		{  60,  99,  12,  51 },		// ０〜９
		{  60,  99,  60,  99 },
		{  60,  99, 108, 147 },
		{  60,  99, 158, 195 },
		{  60,  99, 204, 243 },
		{ 108, 147,  12,  51 },
		{ 108, 147,  60,  99 },
		{ 108, 147, 108, 147 },
		{ 108, 147, 158, 195 },
		{ 108, 147, 204, 243 },

		{ 164, 187,   4,  75 },	// 一番下のボタン３つ
		{ 164, 187,  92, 163 },
		{ 164, 187, 180, 251 },

		{TP_HIT_END,0,0,0},		// 終了データ

	},
};


//------------------------------------------------------------------
/**
 * @brief   タッチパネル検出処理
 *
 * @param   none
 *
 * @retval  u32
 */
//------------------------------------------------------------------
static u32 TouchControl( WBTOWER_WORK *wk )
{
	u32 ret=RECT_HIT_NONE;

	// ３桁の時のタッチ判定
	if(wk->wcw.max==100){
		ret = GF_TP_RectHitTrg(CodeinTouchTbl[0]);
	// ２桁の時
	}else{
		ret = GF_TP_RectHitTrg(CodeinTouchTbl[1]);
	}

	if(ret!=RECT_HIT_NONE){
		OS_Printf("touch=%d\n", ret);
	}
	return ret;
}

// 0 1 2  3 4
// 5 6 7  8 9
// 10  11  12

#define BRANCH1_1	( 13 )
#define BRANCH1_2	( 14 )
#define BRANCH2_1	( 15 )
#define BRANCH2_2	( 16 )

// カーソル移動テーブル(上下左右）
static const u8 cursor_pos_tbl[13][4]={
		{10,  5,  4,  1  }, { 10, 6, 0, 2 }, { 11, 7, 1, 3 }, { 12, 8,2, 4 },{ 12, 9, 3, 0 },
		{ 0, 10,  9,  6, }, {  1,10, 5, 7 }, {  2,11, 6, 8 }, {  3,12,7, 9 },{  4,12, 8, 5 },
		{ BRANCH1_1,  BRANCH1_2, 12, 11  }, {  7, 2, 10, 12, },{  BRANCH2_1, BRANCH2_2, 11, 10 },
};

static const u8 to_cursor[][2]={
	{ 5, 6 },
	{ 0, 1 },
	{ 8, 9 },
	{ 3, 4 },
};

//------------------------------------------------------------------
/**
 * @brief   次のカーソル移動先を取得
 *
 * @param   pos
 * @param   arrow
 * @param   mode
 *
 * @retval  int
 */
//------------------------------------------------------------------
static int next_cursor_pos( int pos, int arrow, u16 *work )
{
	// カーソルの位置保存・復帰
	int next = cursor_pos_tbl[pos][arrow];
	if( next==BRANCH1_1 ){
		next = to_cursor[0][work[0]];
	}else if( next==BRANCH1_2 ){
		next = to_cursor[1][work[0]];
	}else if( next==BRANCH2_1 ){
		next = to_cursor[2][work[1]];
	}else if( next==BRANCH2_2 ){
		next = to_cursor[3][work[1]];
	}else if(next==10){
		if(pos==5 || pos==0 ){
			work[0] = 0;
		}else if(pos==1 || pos==6 ){
			work[0] = 1;
		}
	}else if(next==12){
		if(pos==3 || pos==8 ){
			work[1] = 0;
		}else if(pos==4 || pos==9 ){
			work[1] = 1;
		}
	}

	return next;
}

//------------------------------------------------------------------
/**
 * @brief   入力中の数値は最大数を超えてないか
 *
 * @param   wcw
 * @param   input   -1の場合は足しこまずに現在のワーク値を取得
 *
 * @retval  int		現在値(max値を超えている場合は-1)
 */
//------------------------------------------------------------------
static int num_max_check( WBTOWER_CODEIN_WORK *wcw, int input )
{
#if 0   //aaa
	int i,n[3],num;
	for(i=0;i<3;i++){
		if(wcw->fig[i]<0){
			n[i] = 0;
		}else{
			n[i] = wcw->fig[i];
		}
	}
	n[wcw->point] = input;

	num = n[0]*100+n[1]*10+n[2];

	OS_Printf("num=%d, max=%d\,",num,wcw->max);

	if(num>wcw->max){
		return 0;
	}
	return 1;
#else
    int i, s, n[3], res[3], num, top_zero;
    int keta = 100;
    int keta_max = 3;

    for(i = 0; i < 3; i++){
        n[i] = wcw->fig[i];
    }

    if(input != -1){
        n[wcw->point] = input;
    }

    s = 0;
    top_zero = 0;
    for(i = 0; i < 3; i++){
        if((wcw->fig[i] == 0 && top_zero == 0) || n[i] < 0){
            keta /= 10;
            keta_max--;
        }
        else{
            top_zero = TRUE;
            res[s++] = n[i];
        }
    }

    num = 0;
    for(i = 0; i < keta_max; i++){
        num += res[i] * keta;
        keta /= 10;
    }

	OS_Printf("num=%d, max=%d,",num,wcw->max);

    if(num > wcw->max){
        return -1;
    }
    return num;
#endif
}

#if 0
//--------------------------------------------------------------
/**
 * 入力されている文字をチェックし、マスク設定   aaa
 *
 * @param   wcw
 */
//--------------------------------------------------------------
static void mask_setting( WBTOWER_CODEIN_WORK *wcw )
{
    int now_number, max_offset;
    s8 bttn[10];
    s8 keta_tbl[3] = {100, 10, 1};

	G2_SetWnd0InsidePlane(GX_WND_PLANEMASK_BG0 | GX_WND_PLANEMASK_BG1 |
		GX_WND_PLANEMASK_BG2 | GX_WND_PLANEMASK_BG3 | GX_WND_PLANEMASK_OBJ, TRUE);
	G2_SetWnd1InsidePlane(GX_WND_PLANEMASK_BG0 | GX_WND_PLANEMASK_BG1 |
		GX_WND_PLANEMASK_BG2 | GX_WND_PLANEMASK_BG3 | GX_WND_PLANEMASK_OBJ, TRUE);
	G2_SetWndOutsidePlane(GX_WND_PLANEMASK_BG0 | GX_WND_PLANEMASK_BG1 |
		GX_WND_PLANEMASK_BG2 | GX_WND_PLANEMASK_BG3 | GX_WND_PLANEMASK_OBJ, FALSE);

    now_number = num_max_check(wcw, -1);
    GF_ASSERT(now_number != -1);

    max_offset = wcw->max - now_number;
    now_keta = 3 - wcw->point;

    keta = keta_tbl[wcw->point];
    for(i = 0; i < 10; i++){
        if(i == 0){
            if(now_number + keta * 10 <= wcw->max){
                bttn[i] = TRUE;     //押せる
            }
            else{
                bttn[i] = FALSE;    //押せない
            }
        }
        else{
            if(now_number + keta * i <= wcw->max){
                bttn[i] = TRUE;     //押せる
            }
            else{
                bttn[i] = FALSE;    //押せない
            }
        }
    }

    //上段　０〜４
    //下段　５〜９

    GX_SetVisibleWnd(GX_WNDMASK_W0 | GX_WNDMASK_W1);
	G2_SetWnd0Position(0, 0, 255, 0);
	G2_SetWnd1Position(0, 0, 255, 0);
}
    GX_SetVisibleWnd(GX_WNDMASK_W0 | GX_WNDMASK_W1);
	G2_SetWnd0Position(0, 0, 255, 0);
	G2_SetWnd1Position(0, 0, 255, 0);
#endif

//------------------------------------------------------------------
/**
 * @brief
 *
 * @param   wcw
 *
 * @retval  int
 */
//------------------------------------------------------------------
static int get_codein_num( WBTOWER_CODEIN_WORK *wcw )
{
	int i,n[3],num;
	for(i=0;i<3;i++){
		if(wcw->fig[i]<0){
			n[i] = 0;
		}else{
			n[i] = wcw->fig[i];
		}
	}
	num = n[0]*100 + n[1]*10 + n[2];
	if(num<=0){
		num = 1;
	}

	return num;
}



//------------------------------------------------------------------
/**
 * @brief   未入力部分がないかチェック
 *
 * @param   wcw
 *
 * @retval  int		1:入力されている	0:未入力がある
 */
//------------------------------------------------------------------
static  int no_input_check( WBTOWER_CODEIN_WORK *wcw )
{
	if(wcw->max!=100){
		if( wcw->fig[1]>=0 && wcw->fig[2]>=0 ){
			return 1;
		}
	}else{
		if( wcw->fig[0]>=0 && wcw->fig[1]>=0 && wcw->fig[2]>=0){
			return 1;
		}
	}

	return 0;
}

//------------------------------------------------------------------
/**
 * @brief   入力条件を満たしている場合は「けってい」に移動
 *
 * @param   wcw
 *
 * @retval  int		カーソル位置
 */
//------------------------------------------------------------------
static int end_check( WBTOWER_CODEIN_WORK *wcw )
{
	int flag;
	if( no_input_check( wcw ) ){
		return CODEIN_DECIDE-CODEIN_NUM_0;
	}

	return wcw->cursor;
}


static const u8 code_tbl[][2]={
	 1,  7,
	 7,  7,
	13,  7,
	19,  7,
	25,  7,

	 1, 13,
	 7, 13,
	13, 13,
	19, 13,
	25, 13,


};

//------------------------------------------------------------------
/**
 * @brief   数字OBJのアニメ設定（未入力はアニメしない）
 *
 * @param   wk
 * @param   act		アクターのポインタ
 *
 * @retval  none
 */
//------------------------------------------------------------------
static void  _num_obj_anime( WBTOWER_WORK *wk, int act )
{
	int anm = CLACT_AnmGet( wk->ArrowActWork[act]);

	// 未入力の数値でなければアニメする
//	if(anm!=0){
		ObjButtonAnmSet( wk, act, anm, anm+11, BTOWER_CODEIN_MAIN );
//	}
}


//------------------------------------------------------------------
/**
 * @brief   決定処理（タッチ＆キー共通）
 *
 * @param   wk
 * @param   cursor
 *
 * @retval  none
 */
//------------------------------------------------------------------
static void DecideFunc( WBTOWER_WORK *wk, int cursor )
{
	switch(cursor){
	#if 0   //aaa
	case CODEIN_POINT_100:
		// ２桁の時は反応しない
		if(wk->wcw.max==100){
			wk->wcw.point=0;
			WBTower_PointActPos( wk->PointActWork, wk->wcw.point, wk->wcw.keta );
			Snd_SePlay( WBTOWER_MOVE_SE );
			_num_obj_anime(wk, 0);
		}
		break;
	case CODEIN_POINT_10:
		wk->wcw.point=1;
		WBTower_PointActPos( wk->PointActWork, wk->wcw.point, wk->wcw.keta );
		Snd_SePlay( WBTOWER_MOVE_SE );
		_num_obj_anime(wk, 1);
		break;
	case CODEIN_POINT_1:
		wk->wcw.point=2;
		WBTower_PointActPos( wk->PointActWork, wk->wcw.point, wk->wcw.keta );
		Snd_SePlay( WBTOWER_MOVE_SE );
		_num_obj_anime(wk, 2);

		break;
	#endif
	case CODEIN_NUM_0:	case CODEIN_NUM_1:	case CODEIN_NUM_2:	case CODEIN_NUM_3:	case CODEIN_NUM_4:
	case CODEIN_NUM_5:	case CODEIN_NUM_6:	case CODEIN_NUM_7:	case CODEIN_NUM_8:	case CODEIN_NUM_9:
	#if 0   //aaa
		if(num_max_check(&wk->wcw, cursor-CODEIN_NUM_0)){
	#else
		if(num_max_check(&wk->wcw, cursor-CODEIN_NUM_0) != -1){
	#endif
			// 数値入力
			wk->wcw.fig[wk->wcw.point] = cursor-CODEIN_NUM_0;

			// 入力ポイント移動
			if(wk->wcw.point<2){
				wk->wcw.point++;
			}
			WBTower_PointActPos( wk->PointActWork, wk->wcw.point, wk->wcw.keta );

			// カーソル移動
			wk->wcw.cursor = cursor-CODEIN_NUM_0;
			// 終了条件チェック
			wk->wcw.cursor = end_check( &wk->wcw );
			WBTower_CursorActPos( wk->CursorActWork, wk->wcw.cursor );
			Snd_SePlay( WBTOWER_DECIDE_SE );
			BgButtonAnmSet( wk, code_tbl[cursor-CODEIN_NUM_0][0],
								code_tbl[cursor-CODEIN_NUM_0][1], BTOWER_CODEIN_MAIN );
		}else{
			Snd_SePlay( WBTOWER_FAILED_SE );
		}
	break;
	case CODEIN_MODORU:
		// 「もどる」を表示しているなら処理する
//		wk->wcw.cursor = cursor-CODEIN_NUM_0;
//		WBTower_CursorActPos( wk->CursorActWork, wk->wcw.cursor );
		// 入力数値キャンセル
		wk->wcw.fig[wk->wcw.point] = -1;
		wk->wcw.point--;
		if(wk->wcw.max==100){
			if(wk->wcw.point<0){
				wk->wcw.point=0;
			}
		}else{
			if(wk->wcw.point<1){
				wk->wcw.point=1;
			}

		}
		Snd_SePlay( WBTOWER_DECIDE_SE );
		WBTower_PointActPos( wk->PointActWork, wk->wcw.point, wk->wcw.keta );
		CLACT_AnmChg( wk->ButtonActWork[0], 5 );

		break;
	case CODEIN_DECIDE:
	#if 0   //aaa
		if( no_input_check( &wk->wcw)){
	#else
	    if(1){
	#endif
			wk->wcw.cursor = cursor-CODEIN_NUM_0;
			WBTower_CursorActPos( wk->CursorActWork, wk->wcw.cursor );
		#if 0   //aaa
			wk->wcw.num = get_codein_num( &wk->wcw );
		#else
		    wk->wcw.num = num_max_check( &wk->wcw, -1 );
		    if(wk->wcw.num <= 0){
                wk->wcw.num = 1;
            }
		#endif
			wk->wcw.seq = BTOWER_CODEIN_FADEOUT;
			Snd_SePlay( WBTOWER_DECIDE_SE );
			CLACT_AnmChg( wk->ButtonActWork[1], 5 );
		}else{
			Snd_SePlay( WBTOWER_FAILED_SE );
		}
		break;
	case CODEIN_CANCEL:
		wk->wcw.cursor = cursor-CODEIN_NUM_0;
		wk->wcw.num    = -1;
		WBTower_CursorActPos( wk->CursorActWork, wk->wcw.cursor );
		wk->wcw.seq = BTOWER_CODEIN_FADEOUT;
		Snd_SePlay( WBTOWER_DECIDE_SE );
		CLACT_AnmChg( wk->ButtonActWork[2], 5 );
		break;
	}
}

//------------------------------------------------------------------
/**
 * @brief   パッド移動＆決定処理
 *
 * @param   wk
 *
 * @retval  u32
 */
//------------------------------------------------------------------
static u32 KeyControl( WBTOWER_WORK *wk )
{
	if(sys.repeat&PAD_KEY_UP){
		wk->wcw.cursor = next_cursor_pos(  wk->wcw.cursor, 0, wk->wcw.cur_work );
		WBTower_CursorActPos( wk->CursorActWork, wk->wcw.cursor );
		OS_Printf("cursor=%d\n", wk->wcw.cursor);
		Snd_SePlay( WBTOWER_MOVE_SE );
	}else if(sys.repeat&PAD_KEY_DOWN){
		wk->wcw.cursor = next_cursor_pos( wk->wcw.cursor, 1, wk->wcw.cur_work );
		WBTower_CursorActPos( wk->CursorActWork, wk->wcw.cursor );
		OS_Printf("cursor=%d\n", wk->wcw.cursor);
		Snd_SePlay( WBTOWER_MOVE_SE );

	}else if(sys.repeat&PAD_KEY_LEFT){
		wk->wcw.cursor = next_cursor_pos( wk->wcw.cursor, 2, wk->wcw.cur_work );
		WBTower_CursorActPos( wk->CursorActWork, wk->wcw.cursor );
		OS_Printf("cursor=%d\n", wk->wcw.cursor);
		Snd_SePlay( WBTOWER_MOVE_SE );

	}else if(sys.repeat&PAD_KEY_RIGHT){
		wk->wcw.cursor = next_cursor_pos( wk->wcw.cursor, 3, wk->wcw.cur_work );
		WBTower_CursorActPos( wk->CursorActWork, wk->wcw.cursor );
		OS_Printf("cursor=%d\n", wk->wcw.cursor);
		Snd_SePlay( WBTOWER_MOVE_SE );

	}else if(sys.trg&PAD_BUTTON_DECIDE){
		DecideFunc(wk, wk->wcw.cursor+CODEIN_NUM_0);
	}else if(sys.trg&PAD_BUTTON_CANCEL){
		DecideFunc( wk, CODEIN_MODORU );
	}
	return 0;
}

//------------------------------------------------------------------
/**
 * @brief   数値入力サブメイン
 *
 * @param   WK
 *
 * @retval  int
 */
//------------------------------------------------------------------
static int Codein_main( WBTOWER_WORK *wk )
{
	u32 ret;
	if((ret=TouchControl( wk ))==RECT_HIT_NONE){
		ret=KeyControl(wk);
	}else{
		DecideFunc( wk, ret );
	}

	// 入力済み数値反映
	CodeActShow( wk );

	return CODEIN_SEQ_CONTINUE;

}

//------------------------------------------------------------------
/**
 * @brief   入力済み数値を反映させる処理
 *
 * @param   wk
 *
 * @retval  none
 */
//------------------------------------------------------------------
static void CodeActShow( WBTOWER_WORK *wk )
{
	int i;
	for(i=0;i<3;i++){
		if(wk->wcw.fig[i]==-1){
			CLACT_AnmChg( wk->ArrowActWork[i], 0 );
		}else if(wk->wcw.fig[i]>=0 && wk->wcw.fig[i]<=9){
			CLACT_AnmChg( wk->ArrowActWork[i], wk->wcw.fig[i]+1 );
		}
	}
}


//------------------------------------------------------------------
/**
 * @brief   「これでよろしいですか」
 *
 * @param   wk
 *
 * @retval  int
 */
//------------------------------------------------------------------
static int Codein_decide_mes( WBTOWER_WORK *wk )
{
	return CODEIN_SEQ_CONTINUE;
}
//------------------------------------------------------------------
/**
 * @brief   はい・いいえ開始
 *
 * @param   wk
 *
 * @retval  int
 */
//------------------------------------------------------------------
static int Codein_decide_yesno( WBTOWER_WORK *wk )
{
	return CODEIN_SEQ_CONTINUE;
}
//------------------------------------------------------------------
/**
 * @brief   はい・いいえ待ち
 *
 * @param   wk
 *
 * @retval  int
 */
//------------------------------------------------------------------
static int Codein_decide_yesno_wait( WBTOWER_WORK *wk )
{
	return CODEIN_SEQ_CONTINUE;
}

//------------------------------------------------------------------
/**
 * @brief   入力終了→フェードアウト
 *
 * @param   WK
 *
 * @retval  int
 */
//------------------------------------------------------------------
static int Codein_fadeout( WBTOWER_WORK *wk )
{
	// 下画面フェードアウトスタート
	WIPE_SYS_Start( WIPE_PATTERN_S, WIPE_TYPE_FADEOUT, WIPE_TYPE_FADEOUT, WIPE_FADE_BLACK, 4, 1, HEAPID_WIFI_BATTLETOWER );
	wk->wcw.seq = BTOWER_CODEIN_FADEOUT_WAIT;

	return CODEIN_SEQ_CONTINUE;

}
//------------------------------------------------------------------
/**
 * @brief   フェードアウト待ち
 *
 * @param   WK
 *
 * @retval  int
 */
//------------------------------------------------------------------
static int Codein_fadeout_wait( WBTOWER_WORK *wk )
{
	if(WIPE_SYS_EndCheck()==TRUE){
		// もろもろ解放
		CodeinEnd( wk );

		// 下画面背景復帰
		WBTower_SubBgGraphicSet( wk, 0 );
		// 下画面フェードインスタート
		WIPE_SYS_Start( WIPE_PATTERN_S, WIPE_TYPE_FADEIN, WIPE_TYPE_FADEIN,
						WIPE_FADE_BLACK, 4, 1, HEAPID_WIFI_BATTLETOWER );

		wk->wcw.seq = BTOWER_CODEIN_END_FADEIN_WAIT;
	}

	return CODEIN_SEQ_CONTINUE;

}

//------------------------------------------------------------------
/**
 * @brief   ボタンアニメ待ち
 *
 * @param   wk
 *
 * @retval  int
 */
//------------------------------------------------------------------
static int Codein_button_anm( WBTOWER_WORK *wk )
{
	// OBJ・BGアニメ待ち
	if(ButtonAnmMain( wk )==FALSE){
		wk->wcw.seq = wk->wcw.next;
	}
	return CODEIN_SEQ_CONTINUE;
}


//------------------------------------------------------------------
/**
 * @brief   下画面回複後のフェードイン待ち
 *
 * @param   wk
 *
 * @retval  int
 */
//------------------------------------------------------------------
static int Codein_end_fadein_wait( WBTOWER_WORK *wk )
{
	if(WIPE_SYS_EndCheck()==TRUE){
		wk->wcw.seq = BTOWER_CODEIN_END;
	}
	return CODEIN_SEQ_CONTINUE;
}

//------------------------------------------------------------------
/**
 * @brief   数値入力サブ処理終了
 *
 * @param   WK
 *
 * @retval  int
 */
//------------------------------------------------------------------
static int Codein_end( WBTOWER_WORK *wk )
{

	return CODEIN_SEQ_END;

}

#define BUTTON0_TEXT_X	(  1 )
#define BUTTON1_TEXT_X	( 12 )
#define BUTTON2_TEXT_X	( 23 )
#define BUTTON0_TEXT_Y	( 21 )
#define BUTTON_TEXT_SX	(  8 )
#define BUTTON_TEXT_SY	(  2 )
#define BUTTON0_TEXT_OFFSET	( WBTOWER_MENUFRAME_CHR )
#define SUBLCD_TALK_BMPWIN_OFFSET	( BUTTON0_TEXT_OFFSET+BUTTON_TEXT_SX*BUTTON_TEXT_SY*3)

static const int button_bmpwin_dat[]={
	BUTTON0_TEXT_X, BUTTON1_TEXT_X, BUTTON2_TEXT_X
};



//------------------------------------------------------------------
/**
 * @brief   BMPWIN初期化
 *
 * @param   wk
 *
 * @retval  none
 */
//------------------------------------------------------------------
static void BmpWinInit( WBTOWER_WORK *wk )
{
	int i;

	// ボタンテキスト
	for(i=0;i<3;i++){
		GF_BGL_BmpWinAdd(wk->bgl, &wk->wcw.ButtonMsgWin[i], GF_BGL_FRAME0_S,
		button_bmpwin_dat[i], BUTTON0_TEXT_Y, BUTTON_TEXT_SX, BUTTON_TEXT_SY,
		WBTOWER_TALKFONT_PAL,  BUTTON0_TEXT_OFFSET+BUTTON_TEXT_SX*BUTTON_TEXT_SX*i );
	}

	// 会話ウインドウ
	GF_BGL_BmpWinAdd(wk->bgl, &wk->wcw.MsgSubWin, GF_BGL_FRAME0_S,
		TALK_WIN_X, 		TALK_WIN_Y, 		TALK_WIN_SX, 	TALK_WIN_SY,
		WBTOWER_TALKFONT_PAL,  SUBLCD_TALK_BMPWIN_OFFSET );


}

//------------------------------------------------------------------
/**
 * @brief   BMPWINにメッセージ描画
 *
 * @param   win
 * @param   msgman
 * @param   msgno
 *
 * @retval  none
 */
//------------------------------------------------------------------
static void ButtonMsgDraw( GF_BGL_BMPWIN *win, MSGDATA_MANAGER *msgman, int msgno )
{
	STRBUF *strbuf = MSGMAN_AllocString( msgman, msgno );
	GF_BGL_BmpWinDataFill( win,  0x0000 );
	GF_STR_PrintColor( win, FONT_TOUCH, strbuf, 0, 0, MSG_ALLPUT, GF_PRINTCOLOR_MAKE( 15,2,0 ),NULL);
	GF_BGL_BmpWinOn( win );

	STRBUF_Delete(strbuf);
}




//------------------------------------------------------------------
/**
 * @brief   BMPWIN削除
 *
 * @param   wk
 *
 * @retval  none
 */
//------------------------------------------------------------------
static void BmpWinDelete( WBTOWER_WORK *wk )
{
	int i;
	for(i=0;i<3;i++){
		GF_BGL_BmpWinOff( &wk->wcw.ButtonMsgWin[i] );
		GF_BGL_BmpWinDel( &wk->wcw.ButtonMsgWin[i] );
	}
	GF_BGL_BmpWinDel( &wk->wcw.MsgSubWin );
}

//==============================================================================
/**
 * @brief   数値入力終了処理
 *
 * @param   wk
 *
 * @retval  int
 */
//==============================================================================
static void CodeinEnd( WBTOWER_WORK *wk )
{
    int i;

	// BMPWIN削除
	BmpWinDelete(wk);

	// セルアクターを全部隠す
	CLACT_SetDrawFlag( wk->ArrowActWork[0], 0 );
	CLACT_SetDrawFlag( wk->ArrowActWork[1], 0 );
	CLACT_SetDrawFlag( wk->ArrowActWork[2], 0 );
	CLACT_SetDrawFlag( wk->CursorActWork, 0 );
	CLACT_SetDrawFlag( wk->PointActWork, 0 );
	CLACT_SetDrawFlag( wk->ButtonActWork[0], 0 );
	CLACT_SetDrawFlag( wk->ButtonActWork[1], 0 );
	CLACT_SetDrawFlag( wk->ButtonActWork[2], 0 );

	for(i = 0; i < 3; i++){ //数値は未入力状態のアニメに変更
        CLACT_AnmChg( wk->ArrowActWork[i], 0 );
    }

	return;
}


//--------------------------------------------------------------------------------------------
/**
 * ボタンBG・OBJパレット書き換え
 *
 * @param	wk
 *
 * @retval	"TRUE = アニメ中"
 * @retval	"FALSE = それ以外"
 */
//--------------------------------------------------------------------------------------------
static void ButtonAnmCore( WBTOWER_WORK * wk, u8 pal )
{
	BUTTON_ANM_WORK *bawk = &wk->wcw.bawk;
	if( bawk->mode == BTN_ANM_MODE_OBJ ){
		CLACT_AnmChg( wk->ArrowActWork[bawk->id], pal );
	}else{
		GF_BGL_ScrPalChange(
			wk->bgl, bawk->id,
			bawk->px, bawk->py, bawk->sx, bawk->sy, pal );
		GF_BGL_LoadScreenV_Req( wk->bgl, bawk->id );
	}
}


//------------------------------------------------------------------
/**
 * @brief   ボタンＢＧアニメ処理
 *
 * @param   wk
 *
 * @retval  BOOL
 */
//------------------------------------------------------------------
static BOOL ButtonAnmMain( WBTOWER_WORK * wk )
{
	BUTTON_ANM_WORK * bawk = &wk->wcw.bawk;

	switch( bawk->seq ){
	case 0:
		ButtonAnmCore( wk, bawk->pal1 );
		bawk->seq++;
		break;

	case 1:
		bawk->cnt++;
		if( bawk->cnt != 4 ){ break; }
		ButtonAnmCore( wk, bawk->pal2 );
		bawk->cnt = 0;
		bawk->seq++;
		break;

	case 2:
		bawk->cnt++;
		if( bawk->cnt == 2 ){
			return FALSE;
		}
	}

	return TRUE;
}


//------------------------------------------------------------------
/**
 * @brief   ボタンOBJアニメ発動
 *
 * @param   wk
 * @param   id
 * @param   anm1
 * @param   anm2
 * @param   next
 *
 * @retval  none
 */
//------------------------------------------------------------------
static void ObjButtonAnmSet( WBTOWER_WORK * wk, u8 id, u8 anm1, u8 anm2, u16 next )
{
	wk->wcw.bawk.mode = BTN_ANM_MODE_OBJ;
	wk->wcw.bawk.cnt  = 0;
	wk->wcw.bawk.seq  = 0;
	wk->wcw.bawk.id   = id;
	wk->wcw.bawk.pal1 = anm1;
	wk->wcw.bawk.pal2 = anm2;

	wk->wcw.next = next;
	wk->wcw.seq = BOTWER_CODEIN_BUTTON_ANM;
}

//------------------------------------------------------------------
/**
 * @brief   ボタンＢＧアニメ発動
 *
 * @param   wk
 * @param   px
 * @param   py
 * @param   next
 *
 * @retval  none
 */
//------------------------------------------------------------------
static void BgButtonAnmSet( WBTOWER_WORK * wk, u8 px, u8 py, u16 next )
{
	wk->wcw.bawk.mode = BTN_ANM_MODE_BG;
	wk->wcw.bawk.cnt  = 0;
	wk->wcw.bawk.seq  = 0;
	wk->wcw.bawk.id   = GF_BGL_FRAME1_S;
	wk->wcw.bawk.pal1 = 1;
	wk->wcw.bawk.pal2 = 0;

	wk->wcw.bawk.px = px;
	wk->wcw.bawk.py = py;
	wk->wcw.bawk.sx = 6;
	wk->wcw.bawk.sy = 6;

	wk->wcw.next = next;
	wk->wcw.seq  = BOTWER_CODEIN_BUTTON_ANM;
}